<?php

namespace Util;

class Comm extends \Lib\Util\Comm {

	public static function encryptId($id, $key = 'hongfengd') {
		$data = $id;
		$data = str_pad($data, 8, chr(0));
		$encrypted = mcrypt_encrypt(MCRYPT_3DES, $key, $data, MCRYPT_MODE_ECB);
		$h = 'H16';
		if (strlen($data) > 8) {
			$h = 'H32';
		}
		list(, $hex) = unpack($h, $encrypted);
		$return = 'U' . $hex;
		return $return;
	}

	public static function decryptId($id, $key = 'hongfengd') {
		$hex = substr($id, 1);
		$len = strlen($hex);
		if ($len != 16 && $len != 32) {
			return null;
		}
		$encrypted = pack('H' . $len, $hex);
		$decrypted = mcrypt_decrypt(MCRYPT_3DES, $key, $encrypted, MCRYPT_MODE_ECB);
		return rtrim($decrypted);
	}

	public static function encrypt($string) {
		$key = 'hongfengd';
		return parent::crypt($string, 'E' , $key);
	}

	public static function decrypt($string) {
		$key = 'hongfengd';
		return parent::crypt($string, 'D' , $key);
	}

}